#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

map<vector<int>, int> mp;

bool mycmp(const pair<vector<int>, int> &p1, const pair<vector<int>, int> &p2)
{
    if (p1.second != p2.second)
    {
        return p1.second > p2.second;
    }
    return p1.first < p2.first;
}

int main()
{
    int n, m;
    cin >> n >> m;
    while (n--)
    {
        vector<int> v;
        int input;
        for (int i = 0; i < m; i++)
        {
            cin >> input;
            v.push_back(input);
        }
        mp[v]++;
    }
    cout << mp.size() << endl;
    vector<pair<vector<int>, int>> vp(mp.begin(), mp.end());
    sort(vp.begin(), vp.end(), mycmp);
    for (int i = 0; i < vp.size(); i++)
    {
        cout << vp[i].second;
        for (int j = 0; j < m; j++)
        {
            cout << " " << vp[i].first[j];
        }
        cout << endl;
    }
    return 0;
}